package com.example.calculator;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.googlecode.aviator.AviatorEvaluator;

public class MainActivity extends AppCompatActivity {
    // 1. 定义界面上需要的控件对象
    private TextView tvResult;

    // 需要的变量定义
    private String inputStr;  // 存储输入的数字等的内容的字符串
    private String express;   // 表示真实的表达式
    private boolean flag;     // 清除TextView内容的标志，按了"="之后清除
    private long result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 2. 获取界面的控件对象
        tvResult = findViewById(R.id.tv_result);

        flag = false;
        express = "";
        inputStr = "";
    }

    public void clear(View view) {
        inputStr = "";
        express = "";
        if (!flag && !TextUtils.isEmpty(inputStr)) {
            tvResult.setText(inputStr);
        }
    }

    public void backspace(View view) {
        if (!TextUtils.isEmpty(inputStr)) {
            inputStr = inputStr.substring(0, inputStr.length() - 1);
            express = express.substring(0, express.length() - 1);
        }
        if (!flag && !TextUtils.isEmpty(inputStr)) {
            tvResult.setText(inputStr);
        }
    }

    public void inputDigital(View view) {
        Button button = (Button) view;
        inputStr += button.getText().toString();
        express += button.getText().toString();

        if (!flag && !TextUtils.isEmpty(inputStr)) {
            tvResult.setText(inputStr);
        }
        if (flag) {
            express = button.getText().toString();
            inputStr = button.getText().toString();
            tvResult.setText(inputStr);
            flag = !flag;
        }
    }

    public void inputOperator(View view) {
        Button button = (Button) view;
        if (!TextUtils.isEmpty(inputStr)) {
            inputStr += button.getText().toString();
            if (view.getId() == R.id.btn_multiply) {
                express += "*";
            } else if (view.getId() == R.id.btn_divide) {
                express += "/";
            } else {
                express += button.getText().toString();
            }
            if (!flag && !TextUtils.isEmpty(inputStr)) {
                tvResult.setText(inputStr);
            }
        } else {
            Toast.makeText(this, "先输入数字", Toast.LENGTH_SHORT).show();
        }
    }

    public void compute(View view) {
        if (express.length() > 0) {
            result = (long) AviatorEvaluator.execute(express);
            tvResult.setText(tvResult.getText() + "=" + result);
        }
        flag = true;
        if (!flag && !TextUtils.isEmpty(inputStr)) {
            tvResult.setText(inputStr);
        }
        express = "";
    }

    // 计算完成后的结果可以使用 %
    public void percent(View view) {
        if (TextUtils.isEmpty(express)) {
            tvResult.setText(String.valueOf(result * 0.01));
        } else {
            Toast.makeText(this, "先计算值，再取百分比", Toast.LENGTH_SHORT).show();
        }
    }
}